package utils;

import java.io.File;
import java.io.IOException;
import Book.Book;
import com.bit.utils.FileUtils;
import Constant.Constant;

import java.time.LocalDate;

public class AnalyzingBook {
    public void storeObject(Book[] books, String filename) throws IOException {
        int bookUseLen = 0;

        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                bookUseLen++;
            }
        }
        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < bookUseLen; i++) {
            if (books[i] != null) {
                jsonArray.append("\n");
            }
        }
        FileUtils.writeFile(jsonArray.toString(), filename);
    }

    public Book[] loadObject(String filename) throws IOException {
        String content = FileUtils.readFile(filename);
        if (content == null || content.isEmpty()) {
            System.out.println("图书馆为空！！！！");
            return null;
        }
        String[] bookJsonStrings = content.split("\n");
        Book[] booklist = new Book[bookJsonStrings.length];
        for (int i = 0; i < bookJsonStrings.length; i++) {
            Book book = parseBookJson(bookJsonStrings[i]);
            booklist[i] = book;
        }
        return booklist;
    }

    private Book parseBookJson(String json) {
        String[] pairs = json.split(",");
        int bookId = Integer.parseInt(pairs[0]);
        String title = pairs[1];
        String author = pairs[2];
        String category = pairs[3];
        int publishYear = Integer.parseInt(pairs[4]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        LocalDate shelfDate = LocalDate.parse(pairs[7]);

        if (title != null && author != null && category != null && shelfDate != null) {
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBookId(bookId);
            book.setBorrowed(isBorrowed);
            book.setBorrowedCount(borrowCount);
            return book;
        }
        return null;
    }

    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("java", "gaobo", "编程", 1994, LocalDate.of(2023, 9, 24));
        books[1] = new Book("mysql", "lisi", "编程", 1999, LocalDate.of(2024, 2, 10));
        books[2] = new Book("php", "gaobo", "编程", 2020, LocalDate.of(2023, 9, 23));
        books[3] = new Book("西游记", "吴承恩", "小说", 2024, LocalDate.of(2023, 9, 23));
        AnalyzingBook analyzingBook=new AnalyzingBook();
        try {
            analyzingBook.storeObject(books,Constant.ALL_BOOK_FILE_NAME);
            Book[] ret=analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
            for (int i = 0; i < ret.length; i++) {
                System.out.println(ret[i]);
            }
        } catch(IOException e){
            throw  new RuntimeException(e);
        }
    }
}